/************************************************************************************
 RC Hobby Stream
 Twitter automatic feeder

 First version developed with support of Mapelgate Media

"RC Hobby Stream" is protected by the copyright law
© 2011 Enrico Miglino / Tech Consulting (enrico.miglino@ovi.com)
Distributed under the LGPL 3.0 license.

This file is part of "RC Hobby Stream".

    "RC Hobby Stream" is free software: you can redistribute it
    and/or modify it under the terms of the Lesser GNU General Public License
    as published by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "RC Hobby Stream" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with "RC Hobby Stream".
    If not, see <http://www.gnu.org/licenses/>.
************************************************************************************/

import Qt 4.7

/*  Tweet component
    The listDelegate component instantiate all the elements that should be contained in every
    single tweet message.
    As component, this element is reusable, so it can be applied in the tweet list for all the
    effective elements delivered by the twitter site.
    On top of the list there is the logic set by the search: as a matter of fact, the effective
    messeges that match with the search are those that are presented in the list reiterating
    components.
    The item contains both the object definition and related elements.
*/

Component {
    id: listDelegate    // Definition of the component used to present the message list

    Item {
        id: wrapper;

        property int iconSize: 32   // Size of the square image to the left of every message (user avatar)

        width: wrapper.ListView.view.width; // dinamic width of the list
        // Dinamic height of textarea is calculated, based on the height (lenght) of the text
        // It should be at least the same of the height of the image.
        height: if(txt.height > iconSize ){txt.height + 10} else {iconSize}

        /*  handleLink(link)
            Manages the links in the twitter message: if it is a usr (http protocol)
            when clicked the link is opened in the default browser.
        */
        function handleLink(link){
            if(link.slice(0,3) == 'app') {
                screen.setUser(link.slice(7));
            }
            else if(link.slice(0,4) == 'http') {
                Qt.openUrlExternally(link);
            }
        }

        /*  addTags(str)
            Convert the elements in the string corresponding to links in html tags.
        */
        function addTags(str){
            // Replace the user @user with the link to the user message list (if it is defined)
            var ret = str.replace(/@[a-zA-Z0-9_]+/g, '<a href="app://$&">$&</a>');
           // Chain previous string to surround http links with html link tags
            var ret2 = ret.replace(/http:\/\/[^ \n\t]+/g, '<a href="$&">$&</a>');
            return ret2;
        }

        /*  userName(str)

            Strip away paranthesis from the user name
        */
        function userName(str) {
            var user = str.replace(/\([\S|\s]*\)/gi, "");
            return user.trim();
        }

        Item {
            id: moveMe; // Definition of the item used for list scroll

            height: parent.height

            Rectangle {
                id: textRect   // Empty rectangle to fit message text

                color: "#f4f0f0";
                opacity: wrapper.ListView.index % 2 ? 0.2 : 0.3;
                height: wrapper.height-2;
                width: wrapper.width;
                y: 1
            }

            Text {
                id:txt; // Twitter message text
                // Text message position (relative to the user icon)
                y:4; x: wrapper.iconSize - 4
                // Create the message string in html format with linkable elements
                text: '<html><style type="text/css">a:link {color:"#38a4be"}; a:visited {color:"#0a5568"}</style>'
                    + '<a href="app://@'+userName(name)+'"><b>'+userName(name) + "</b></a> from " +source
                    + "<br /><b>" + statusText + "</b></html>";
                textFormat: Qt.RichText
                color: "#525455"; style: Text.Normal; wrapMode: Text.WordWrap
                font.family: "Arial"; font.pointSize: 8; font.weight: Font.Light
                anchors.left: image.right; anchors.right: textRect.right; anchors.leftMargin: 6; anchors.rightMargin: 6
                onLinkActivated: wrapper.handleLink(link)
            }

            Image {
                id: image; // Definition of the image associated with a message. This is the "loading" image.

                // Workaround to solve an issue that don't work correctly in the desktop version
                // under windows.
                // The windows (XP and 7) compiled version only (... only!) don't show the 'userImage'
                // that is a link to the user icon image on Twitter.
                // To solve this problem, userImage is dynamically created from the user name of the message
                // and loded locally at runtime.
                source: "../images/" + name + ".png"
                opacity: 1
                x: 6; width: wrapper.iconSize; height: wrapper.iconSize; smooth: true
                anchors.verticalCenter: parent.verticalCenter
            }

        }
    }
}
